package de.tu_darmstadt.gdi.battleship;

import java.util.Vector;

/**
 * This class represents all ships and deliverers some functions for the logic.
 * 
 * @author Marcel Andreas Gazsi
 * 
 */
public abstract class Ship {
	public Vector<Section> sections;
	public int[] start;
	public int[] end;

	/**
	 * Standard constructor for all ships. It creates the correct amount of
	 * Sections for each type and sets there coordinats.
	 * 
	 * @param countSections
	 *            int The count of Sections which the ship have.
	 * @param start
	 *            int[] The first section coordinate.
	 * @param end
	 *            int[] The last section coordinate.
	 * @throws Exception
	 *             Is thrown when the ship does't lay in the same row or in the
	 *             same column.
	 */
	public Ship(int countSections, int[] start, int[] end) throws Exception {
		sections = new Vector<Section>();
		this.start = new int[2];
		this.start = start;
		this.end = new int[2];
		this.end = end;

		for (int i = 0; i < countSections; i++) {
			// Ausführen, wenn es Wasser ist, da Wasser nur eine Sektion hat.
			if (countSections == 1) {
				sections.add(new Section(start[0], start[1], '-'));
			} else {
				if (start[0] == end[0]) {
					if (i == 0)
						sections.add(new Section(start[0], start[1] + i, 'l'));
					if (i == countSections-1)
						sections.add(new Section(start[0], start[1] + i, 'r'));
					else if(i > 0 && i < countSections)
						sections.add(new Section(start[0], start[1] + 1, 'h'));
				}
				// Schiff liegt vertikal
				else if (start[1] == end[1]) {
					if (i == 0)
						sections.add(new Section(start[0] + i, start[1], 't'));
					if (i  == countSections-1)
						sections.add(new Section(start[0] + 1, start[1], 'b'));
					else if(i > 0 && i < countSections)
						sections.add(new Section(start[0] + 1, start[1], 'v'));
				} else
					throw new Exception(
							"Die Platzierung des Schiffes ist ungültig! Es liegt nicht vertikal oder horizontal.");
			}
		}
	}

	/**
	 * This function checks if a section of it was hit, if so it will mark the
	 * section as hit and returns true, otherwise false
	 * 
	 * @param x
	 *            int The x coordinate
	 * @param y
	 *            int The y coordinate
	 * @return boolean True if a section was hit, else false.
	 */
	public boolean shipWasHit(int x, int y) {
		if (x == start[0] && y <= end[1]) {
			for (Section temp : this.sections) {
				if (x == temp.x && y == temp.y) {
					temp.wasHit();
					return true;
				}
			}
		} else if (y == start[1] && x <= end[0]) {
			for (Section temp : this.sections) {
				if (x == temp.x && y == temp.y) {
					temp.wasHit();
					return true;
				}
			}
		} else
			return false;
		// Wenn er bis hier her durch gelaufen ist, wurde keine passende Section
		// gefunden, daher wird false zurück gegeben.
		return false;
	}
	/**
	 * for TestAdapter
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean isShip(int x, int y){
		if (x == start[0] && y <= end[1]) {
			for (Section temp : this.sections) {
				if (x == temp.x && y == temp.y) {
					return true;
				}
			}
		} else if (y == start[1] && x <= end[0]) {
			for (Section temp : this.sections) {
				if (x == temp.x && y == temp.y) {
					return true;
				}
			}
		} else
			return false;
		// Wenn er bis hier her durch gelaufen ist, wurde keine passende Section
		// gefunden, daher wird false zurück gegeben.
		return false;
	}

}
